package com.qys.wallet.pay.hyperbc;


import com.alibaba.fastjson2.JSONObject;
import com.qys.wallet.pay.hyperbc.dto.resp.CallBackDTO;
import com.qys.wallet.pay.hyperbc.dto.resp.HyWlCollBackDTO;
import com.qys.wallet.pay.hyperbc.util.RsaSignerUtils;
import com.qys.wallet.service.WalletCoinAddrService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class HyperbcCallBackService {

    private final HyperbcConfig hyPayConfig;
    private final WalletCoinAddrService coinAddrService;

    /**
     * @param dto  回调参数
     * @param type 类型  1：充值   2：提现
     * @return
     */
    public String handleCallback(CallBackDTO dto, int type) {
        HyWlCollBackDTO data = dto.getData();
        log.info("接受到{}回调-->【主链[{}],币种[{}],txid[{}],地址[{}],数量[{}],总额（amount+fee）[{}]】", type == 1 ? "充值" : "提现",
                data.getChain(), data.getCoin(), data.getTxid(), data.getAddress(), data.getAmount(), data.getTotal());
        boolean flag = true;
        if (type == 1) {
            flag = coinAddrService.rechargeCallBack(data);
        } else {
            flag = coinAddrService.withdrawalCallBack(data);
        }
        JSONObject res = new JSONObject();
        JSONObject resData = new JSONObject();
        Map<String, Object> params = new HashMap<>();
        //根据地址查询到充值人
        if (flag) {
            res.put("status", 200);
            resData.put("success_data", "success");
            params.put("success_data", "success");
        } else {
            res.put("status", 500);
            resData.put("success_data", "fail");
            params.put("success_data", "fail");
        }
        try {
            res.put("sign", RsaSignerUtils.genSign(params, hyPayConfig.getPriKey()));
        } catch (Exception e) {
        }
        res.put("data", resData);
        return res.toJSONString();
    }
//
//    public Map<String, Object> riskControlCallback(CallBackDTO dto) {
//        JSONObject res = new JSONObject();
//        res.put("status", 200);
//        HyWlReCollBackDTO data = dto.getData();
//        String userId = data.getUser_id();
//        if (StringUtils.isBlank(userId)) {
//            throw new RuntimeException("缺少业务单号");
//        }
//        MallUserAccountWithdrawal withdrawal = withdrawalService.getById(data.getOrder_id());
//        if (withdrawal == null) {
//            res.put("status", 5402);
//        }
//        JSONObject resData = new JSONObject();
//        resData.put("success_data", "success");
//        Map<String, Object> params = new HashMap<>();
//        params.put("order_id", data.getOrder_id());
//        params.put("status_code", res.get("status"));
//        params.put("timestamp", Long.toString(System.currentTimeMillis() / 1000));
//        try {
//            res.put("sign", RsaSignerUtils.genSign(params, hyPayConfig.getRePriKey()));
//        } catch (Exception e) {
//            log.error("风控-签名失败");
//        }
//        res.put("data", resData);
//        Map<String, Object> map = new HashMap<>();
//        map.put("status", res.get("status"));
//        map.put("data", params);
//        map.put("sign", res.get("sign"));
//        System.out.println(params);
//        return map;
//
//    }
}
